package com.etcbase.dcs.controller.main;

import java.util.HashMap;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.etcbase.dcs.R;
import com.etcbase.dcs.controller.camera.CameraFragment;
import com.etcbase.dcs.controller.lookup.AdvancedSearchFragment;
import com.etcbase.dcs.controller.lookup.ServiceFileFormPanelFragment;
import com.etcbase.dcs.controller.map.MapNavigationFragment;
import com.etcbase.dcs.controller.sfctask.GroupTaskFragment;
import com.etcbase.dcs.controller.sfctask.HistoryTaskFragment;
import com.etcbase.dcs.controller.sfctask.UserTaskFragment;
import com.etcbase.dcs.controller.sfctask.WatchTaskFragment;
import com.etcbase.dcs.utils.gui.LayoutWidthAnimator;

@SuppressLint("ParserError")
public class MainActivity extends Activity implements OnClickListener {
	private static final String TAG = "MainActivity";

	// TODO: find a better solution for these hard-coded values
	private int leftPanelOpeningDuration = 500;
	private int leftPanelMaxWidth;

	// GUI COMPONENTS
	private ImageButton hideShowLeftPanelImageButton;
	private LinearLayout leftPanelLinearLayout;
	private HashMap<Integer, Fragment> fragmentHashMap;
	private LayoutWidthAnimator leftMenuAnimator;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_panel);

		// hide the keyboard when not needed
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		hideShowLeftPanelImageButton = (ImageButton) findViewById(R.id.mainImageButtonHideShowLeftPanel);
		leftPanelLinearLayout = (LinearLayout) findViewById(R.id.mainlinearLayoutleftPanel);
		leftPanelMaxWidth = getResources().getDimensionPixelSize(
				R.dimen.mainLeftPanelWidth);

		leftMenuAnimator = new LayoutWidthAnimator(leftPanelLinearLayout,
				leftPanelOpeningDuration);

		hideShowLeftPanelImageButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				LayoutParams params = leftPanelLinearLayout.getLayoutParams();
				Log.d(TAG, "layout width = " + params.width);

				if (params.width == 0) {
					showLeftMenu();
				} else {
					hideLeftMenu();
				}
			}
		});

		// TOP LAYOUT
		findViewById(R.id.mainButtonTopMenuAdvenceSearch).setOnClickListener(
				this);
		// findViewById(R.id.mainButtonTopMenuMap).setOnClickListener(this);
		// findViewById(R.id.mainButtonTopMenuCamera).setOnClickListener(this);
		// findViewById(R.id.mainButtonSearchText).setOnClickListener(this);

		// LEFT LAYOUT
		fragmentHashMap = new HashMap<Integer, Fragment>();
		fragmentHashMap.put(R.id.leftMenuButtonUserTaskFragment,
				new UserTaskFragment());
		fragmentHashMap.put(R.id.leftMenuButtonGroupTaskFragment,
				new GroupTaskFragment());
		fragmentHashMap.put(R.id.leftMenuButtonHistoryTaskFragment,
				new HistoryTaskFragment());
		fragmentHashMap.put(R.id.leftMenuButtonWatchTaskFragment,
				new WatchTaskFragment());
		fragmentHashMap.put(R.id.leftMenuButtonMapFragment,
				new MapNavigationFragment());
		fragmentHashMap.put(R.id.leftMenuButtonCameraFragment,
				new CameraFragment());

		// TOP MENU
		// fragmentHashMap = new HashMap<Integer, Fragment>();
		fragmentHashMap.put(R.id.mainButtonTopMenuAdvenceSearch,
				new AdvancedSearchFragment());
		fragmentHashMap.put(R.id.mainButtonTopMenuMap,
				new MapNavigationFragment());
		fragmentHashMap.put(R.id.mainButtonTopMenuCamera, new CameraFragment());

		// bind button-related fragments to the listener
		for (int id : fragmentHashMap.keySet()) {
			findViewById(id).setOnClickListener(this);
		}

		// INVOKED FRAGMENTS
		fragmentHashMap.put(R.layout.service_file_form_panel,
				new ServiceFileFormPanelFragment());

		// set the activity state
		activityState = new ActivityState();
		if (savedInstanceState == null) {
			activityState.leftMenuVisible = false;
			activityState.centerFragmentID = R.id.leftMenuButtonUserTaskFragment;
		} else {
			activityState.get(savedInstanceState);
		}
		RestoreActivityState();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		Log.d(TAG, "onSaveInstanceState");
		activityState.put(outState);
		super.onSaveInstanceState(outState);
	}

	// this tells what to do when layout animations are done in this class
	private AnimatorListener leftMenuAnimatorListener = new AnimatorListener() {
		@Override
		public void onAnimationStart(Animator animation) {
			hideShowLeftPanelImageButton.setClickable(false);
		}

		@Override
		public void onAnimationRepeat(Animator animation) {
		}

		@Override
		public void onAnimationEnd(Animator animation) {
			hideShowLeftPanelImageButton.setClickable(true);
			ViewGroup.LayoutParams params = leftPanelLinearLayout
					.getLayoutParams();
			if (params.width > 0) {
				hideShowLeftPanelImageButton
						.setImageResource(android.R.drawable.ic_media_rew);
			} else {
				hideShowLeftPanelImageButton
						.setImageResource(android.R.drawable.ic_media_ff);
			}
		}

		@Override
		public void onAnimationCancel(Animator animation) {
		}
	};

	private void toast(String text) {
		Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
	}

	private void showLeftMenu() {
		leftMenuAnimator.start(leftPanelMaxWidth, leftMenuAnimatorListener);
		activityState.leftMenuVisible = true;
	}

	private void hideLeftMenu() {
		leftMenuAnimator.start(0, leftMenuAnimatorListener);
		activityState.leftMenuVisible = false;
	}

	@Override
	public void onClick(View v) {

		// check if one of the fragments buttons is pressed
		int id = v.getId();
		setCenterFragment(id);
	}

	public void setCenterFragment(int id) {
		Fragment leftMenuFragment = fragmentHashMap.get(id);
		if (leftMenuFragment != null) {
			setCenterFragment(leftMenuFragment, id);
			hideLeftMenu();
		}
	}

	private void setCenterFragment(Fragment fragment, int id) {

		if (activityState.centerFragmentID == id) {
			Log.d(TAG, "fragment already set!");
			return;
		}

		FragmentManager fragmentManager = getFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager
				.beginTransaction();

		fragmentTransaction.replace(R.id.mainFrameLayoutCenter, fragment,
				fragment.getClass().getSimpleName());
		fragmentTransaction.commit();

		activityState.centerFragmentID = id;
	}

	// this class is used to restore the state of the activity
	ActivityState activityState;

	class ActivityState {
		public int centerFragmentID = -1;
		public boolean leftMenuVisible = false;
		boolean showLeftMenu;

		void put(Bundle bundle) {
			bundle.putInt("centerFragmentID", centerFragmentID);
			bundle.putInt("leftMenuVisible", leftMenuVisible ? 1 : 0);
		}

		void get(Bundle bundle) {
			centerFragmentID = bundle.getInt("centerFragmentID");
			showLeftMenu = leftMenuVisible = (bundle.getInt("leftMenuVisible") == 0 ? false
					: true);
			// showLeftMenu = leftMenuVisible;
		}
	}

	void RestoreActivityState() {
		Log.d(TAG, "RestoreActivityState");
		setCenterFragment(activityState.centerFragmentID);

		// if( activityState.leftMenuVisible ) {
		if (activityState.showLeftMenu) {
			showLeftMenu();
		} else {
			hideLeftMenu();
		}
	}
}
